# Load extensions for Helm
load('ext://helm_remote', 'helm_remote')

# Increase apply timeout
update_settings(k8s_upsert_timeout_secs=300)

# 1. Define and apply the dapr-system namespace YAML
print("Defining dapr-system namespace YAML")
k8s_yaml(local('kubectl create namespace dapr-system --dry-run=client -o yaml'))

# 2. Install Dapr runtime via Helm into the namespace
print("Deploying Dapr Core Helm chart")
helm_remote(
    chart='dapr',
    repo_url='https://dapr.github.io/helm-charts/',
    repo_name='dapr',
    version='1.15',
    release_name='dapr',
    namespace='dapr-system',
    set=['global.mtls.enabled=false'],
)

# 3. Install Dapr Dashboard via Helm
print("Deploying Dapr Dashboard Helm chart")
helm_remote(
    chart='dapr-dashboard',
    repo_url='https://dapr.github.io/helm-charts/',
    repo_name='dapr',
    release_name='dapr-dashboard',
    namespace='dapr-system'
)

# 5. Manage Dapr Dashboard Service (created by 'dapr-dashboard' Helm chart) for Port Forwarding
# ADD Port Forwarding configuration TO the 'dapr-dashboard' resource.
print("Configuring Dapr Dashboard port-forward")
k8s_resource(
    'dapr-dashboard', # TARGET the existing resource named 'dapr-dashboard' (from Helm release)
    port_forwards=[port_forward(local_port=8080, container_port=8080, name='dapr-dashboard-ui')],
    labels=['dapr-ui'] # Add or update labels for the 'dapr-dashboard' resource
)


# 6. Deploy Redis via Helm into the default namespace
# This creates a Tilt resource named 'redis' (from release_name)
print("Deploying Redis Helm chart")
helm_remote(
    chart='redis',
    repo_url='https://charts.bitnami.com/bitnami',  # Add the Dapr Helm repository
    repo_name='bitnami',
    release_name='redis',
    namespace='default',
    set=['auth.enabled=false']
)

# 7. Apply Dapr components (State Store, PubSub, Subscription) into the default namespace
k8s_yaml('./components/redis-state.yaml')  # Directly reference the YAML file
k8s_yaml('./components/redis-pubsub.yaml')  # Directly reference the YAML file
k8s_yaml('./components/subscriptions.yaml')  # Directly reference the YAML file